<template>
  <div>
    <el-dialog :title="ruleForm.id == '' ? '添加巡检设备' : '编辑巡检设备'" :visible.sync="addEditIsDialog_c" :close-on-click-modal="false" @close="resetForm('formName')" width="800px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="140px" class="demo-ruleForm" :class="{ 'form-rules-show': seeType }">
        <el-row>
          <el-col :span="24">
            <el-form-item label="考场组织：" prop="examinationRoomId">
              <el-cascader v-model="treeId" placeholder="请选择考场组织" :options="schoolArray" :props="{
                  checkStrictly: false,
                  value: 'id',
                  label: 'name',
                  children: 'nextDatas',
                }" @change="changeTreeItem" clearable></el-cascader>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备编码：" prop="code">
              <el-input v-model="ruleForm.code" placeholder="请输入设备编码" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备名称：" prop="name">
              <el-input v-model="ruleForm.name" placeholder="请输入设备名称" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="FTPIP：" prop="ip">
              <el-input v-model="ruleForm.ip" placeholder="请输入FTPIP" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="FTP端口：" prop="port">
              <el-input-number v-model="ruleForm.port" placeholder="请输入FTP端口" :precision="0" :step="1" :max="9999" :min="0" :disabled="seeType"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="FTP账号：" prop="username">
              <el-input v-model="ruleForm.username" placeholder="请输入FTP账号" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="FTP密码：" prop="password">
              <el-input v-model="ruleForm.password" placeholder="请输入FTP密码" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="平面图片路径：" prop="imagePath">
              <el-input v-model="ruleForm.imagePath" placeholder="请输入平面图片路径" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="平面图片名称：" prop="imageName">
              <el-input v-model="ruleForm.imageName" placeholder="请输入平面图片名称" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="点位文件路径：" prop="pointPath">
              <el-input v-model="ruleForm.pointPath" placeholder="请输入点位文件路径" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="点位文件名称：" prop="pointName">
              <el-input v-model="ruleForm.pointName" placeholder="请输入点位文件名称" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="网络摄像头：" prop="monitorVideoId">
              <el-select v-model="ruleForm.monitorVideoId" placeholder="请选择网络摄像头" filterable clearable :disabled="seeType">
                <el-option v-for="item in videoMonitorArray" :key="item.id" :label="item.videoName" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="红外摄像头：" prop="infraredVideoId">
              <el-select v-model="ruleForm.infraredVideoId" placeholder="请选择红外摄像头" filterable clearable :disabled="seeType">
                <el-option v-for="item in videoMonitorArray" :key="item.id" :label="item.videoName" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="排序号：" prop="orderNum">
              <el-input-number v-model="ruleForm.orderNum" placeholder="请输入排序号" :precision="0" :step="1" :max="9999" :min="0" :disabled="seeType"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button type="primary" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { getInspectionDevice, saveInspectionDevice } from '@/api/inspection'
import { getSchoolInfos } from '@/api/school'
import { getVideoMonitors } from '@/api/video'

export default {
  components: {},
  props: {
    addEditId: {
      type: String,
      default: ''
    },
    addEditStatus: {
      type: String,
      default: 'add'
    },
    addEditIsDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: '',
        examinationRoomId: '',
        code: '',
        name: '',
        ip: '',
        port: '',
        username: '',
        password: '',
        imagePath: '',
        imageName: '',
        pointPath: '',
        pointName: '',
        monitorVideoId: '',
        infraredVideoId: '',
        orderNum: ''
      },
      rules: {
        // 表单规则
        examinationRoomId: [{ required: true, message: '请选择考场组织', trigger: 'change' }],
        code: [{ required: true, message: '请输入设备编码', trigger: 'blur' }],
        name: [{ required: true, message: '请输入设备名称', trigger: 'blur' }],
        ip: [{ required: true, message: '请输入FTPIP', trigger: 'blur' }],
        port: [{ required: true, message: '请输入FTP端口', trigger: 'blur' }],
        username: [{ required: true, message: '请输入FTP账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入FTP密码', trigger: 'blur' }],
        imagePath: [{ required: true, message: '请输入平面图片路径', trigger: 'blur' }],
        imageName: [{ required: true, message: '请输入平面图片名称', trigger: 'blur' }],
        pointPath: [{ required: true, message: '请输入点位文件路径', trigger: 'blur' }],
        pointName: [{ required: true, message: '请输入点位文件名称', trigger: 'blur' }],
        monitorVideoId: [{ required: true, message: '请选择网络摄像头', trigger: 'change' }],
        infraredVideoId: [{ required: true, message: '请选择红外摄像头', trigger: 'change' }],
        orderNum: [{ required: true, message: '请输入排序号', trigger: 'blur' }]
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      treeId: [], // 考场组织id集合
      schoolArray: [], // 考场组织集合
      videoMonitorArray: [] // 网络摄像头集合
    }
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val
      if (this.addEditIsDialog_c) {
        // 添加
        if (this.addEditStatus == 'add') {
          this.ruleForm.id = ''
          this.seeType = false
        }

        // 编辑
        if (this.addEditStatus == 'edit') {
          this.ruleForm.id = this.addEditId
          this.seeType = false
          this.getDetail()
        }

        // 查看
        if (this.addEditStatus == 'see') {
          this.ruleForm.id = this.addEditId
          this.seeType = true
          this.getDetail()
        }

        // 获取考场组织
        this.getSchoolInfos()
      }
    }
  },
  methods: {
    // 详情
    getDetail() {
      getInspectionDevice({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          this.ruleForm = res.data
          this.treeId = [res.data.examinationRoomId, res.data.collegeId, res.data.facultyId, res.data.floorId, res.data.examinationRoomId]

          // 获取网络摄像头列表
          this.getVideoMonitorList()
        }
      })
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveInspectionDevice(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg
              })

              // 关闭弹窗
              this.$parent.closeAddEdit()
              // 刷新列表
              this.$parent.onSubmit()
            }
          })
        }
      })
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.treeId = []
      this.$parent.closeAddEdit()
    },
    // 获取考场组织
    getSchoolInfos() {
      getSchoolInfos().then((res) => {
        if (res.code == 200) {
          this.schoolArray = res.data
        }
      })
    },
    // 考场组织下拉切换
    changeTreeItem(val) {
      // 清空
      this.ruleForm.monitorVideoId = ''
      this.ruleForm.infraredVideoId = ''
      this.videoMonitorArray = []

      let len = val.length
      if (val && len == 5) {
        this.ruleForm.examinationRoomId = val[len - 1]

        // 获取网络摄像头列表
        this.getVideoMonitorList()
      } else {
        this.ruleForm.examinationRoomId = ''
        this.treeId = []
      }
    },
    // 获取网络摄像头列表
    getVideoMonitorList() {
      if (!this.ruleForm.examinationRoomId || this.ruleForm.examinationRoomId == '') {
        return
      }

      getVideoMonitors({ id: this.ruleForm.examinationRoomId }).then((res) => {
        if (res.code == 200) {
          this.videoMonitorArray = res.data
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;

  .el-input-number {
    width: 100%;
  }

  .el-cascader {
    width: 100%;
  }
}
</style>
